<?php
defined('IN_SITE') or exit('Access Denied');

define('MOD_ROOT', WWW_ROOT.''.$module.'/');
$FORMGUIDE = cache_read('formguide.php');

require_once MOD_ROOT.'formguide/formguide_admin.class.php';
require_once MOD_ROOT.'formguide/formguide_fields.class.php';

$formguide_admin = new formguide_admin();

if(!$action) $action = 'manage';
if(!$forward) $forward = "?module=$mod&file=$file&action=manage";



// fileds
if(!$forward) $forward = HTTP_REFERER;
if(!$action) $action = 'manage';
if(!class_exists('formguide_admin'))
{
	require MOD_ROOT.'admin/include/formguide_admin.class.php';
}
$formguide_admin = new formguide_admin();

if(!class_exists('formguide_fields'))
{
	require MOD_ROOT.'formguide_fields.class.php';
}
$formguide_fields = new formguide_fields($formid);
require_once MOD_ROOT.'formguide/fields/patterns.inc.php';
require 'fields/fields.inc.php';

$tablename = DB_PRE.'form_'.$FORMGUIDE[$formid]['tablename'];

$submenu = array(
	array('添加字段', '?module='.$module.'&file='.$file.'&action=add&formid='.$formid),
	array('管理字段', '?module='.$module.'&file='.$file.'&action=manage&formid='.$formid),
	array('预览表单', '?module='.$module.'&file='.$file.'&action=preview&formid='.$formid),
);
$menu = admin_menu('字段管理', $submenu);
if(!$forward) $forward = "?module=$mod&file=$file&action=manage";
// end


//view
if(!class_exists('formguide_admin'))
{
	require MOD_ROOT.'admin/include/formguide_fields.class.php';
}
$formguide_fields = new formguide_fields($formid);

if(!class_exists('formguide_form'))
{
	require CACHE_MODEL_PATH.'formguide_form.class.php';
}
$formguide_form = new formguide_form($formid);

if(!class_exists('formguide_output'))
{
	require CACHE_MODEL_PATH.'formguide_output.class.php';
}
$formguide_output = new formguide_output($formid);

if(!$action) $action = 'manage';
if(!$forward) $forward = '?module='.$module.'&file='.$file.'&action=manage&formid='.$formid;
//end


switch($action)
{
	case 'add':
		if($dosubmit)
		{
			$formid = $formguide_admin->add($info, $setting);
			if(!$formid)
			{
				showmessage($formguide_admin->msg(), $forward);
			}
			$formguide_fields = new formguide_fields($formid);
			$formguide_fields->cache();
			showmessage('操作成功', $forward.'&formid='.$formid);
		}
		else
		{
			
			include 'templates/add.tpl.php';
		}
	break;
	case 'edit':
		if($dosubmit)
		{
			$formguide_admin->edit($formid, $info, $setting);
			$formguide_fields = new formguide_fields($formid);
			$formguide_fields->cache();
			showmessage('操作成功', $forward);
		}
		else
		{
			$result = $formguide_admin->get($formid);
			@extract(new_htmlspecialchars($result));
			@extract($setting);
			if($starttime) $starttime = date('Y-m-d', $starttime);
			if($endtime) $endtime = date('Y-m-d', $endtime);
			include 'templates/edit.tpl.php';
		}
	break;
	case 'manage':
		if($formname) $where = " name LIKE '%$formname%'";
		$pagesize = $SETTING['pagesize'] ? $SETTING['pagesize'] : 30;
		if(!$order) $order = ' formid DESC';
		$arr_info = $formguide_admin->listinfo($where, $order, $page, $pagesize);
		include 'templates/manage.tpl.php';
	break;
	case 'delete':
		if(empty($formid)) showmessage('请选择要删除的表单');
		$formguide_admin->delete($formid);
		showmessage('操作成功', "?module=formguide&file=$file&action=manage");
	break;
	case 'disabled':
		$formguide_admin->disabled($formid, $val);
		$formguide_fields = new formguide_fields($formid);
		$formguide_fields->cache();
		showmessage('操作成功', $forward);
	break;
	case 'checktable':
		if(!preg_match("/^[a-z0-9_][a-z0-9_]+$/", $value))
		{
			exit('字符只能包含英文字母，数字与下划线且必须为小写');
		}
		elseif ($formguide_admin->check_tablename($value, $formid)) 
		{
			exit('表名已经存在');
		}
		else
		{
			exit('success');
		}
	break;
	case 'checkmodel';
		if ($formguide_admin->check_formname($value, $formid))
		{
			exit('表单名称已经存在');
		}
		else
		{
			exit('success');
		}
	break;
	
	case 'stat':
		$r = $db->get_one("SELECT `name`,`tablename` FROM ".DB_PRE."formguide WHERE formid='$formid'");
		$formname = $r['name'];
		$tablename = $r['tablename'];
		$infos = $datas = array();
		$result = $db->query("SELECT * FROM ".DB_PRE."formguide_fields WHERE formid='$formid' AND formtype='box'");
		while($r = $db->fetch_array($result))
		{
			$infos[] = $r;
		}
		$result = $db->query("SELECT * FROM ".DB_PRE."form_$tablename");
		while($r = $db->fetch_array($result))
		{
			$datas[] = $r;
		}
		include 'templates/stat.tpl.php';
		break;
		
	//
	case 'fields_add':
		if($dosubmit)
		{
			$info['formid'] = $formid;
			$info['unsetgroupids'] = isset($unsetgroupids) ? implodeids($unsetgroupids) : '';
			if(!$result = $formguide_fields->add($info, $setting))
			{
				showmessage($formguide_fields->msg, 'goback');
			}
			if($result)
			{
				extract($setting);
				extract($info);
				require_once 'fields/'.$formtype.'/field_add.inc.php';
				//require_once MOD_ROOT.'formguide/createhtml.php';
				showmessage('操作成功！', $forward);
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		else 
		{
			//if(!is_ie()) showmessage('本功能只支持IE浏览器，请用IE浏览器打开。');
			$unsetgroups = form::checkbox($GROUP, 'unsetgroupids', 'unsetgroupids', '', 5);
			include 'templates/fields_add.tpl.php';
		}
		break;
	case 'fields_edit':
		if($dosubmit)
		{
			$info['unsetgroupids'] = isset($unsetgroupids) ? implodeids($unsetgroupids) : '';
			$result = $formguide_fields->edit($fieldid, $info, $setting);
			if($result)
			{
				extract($setting);
				extract($info);
				require_once MOD_ROOT.'formguide/fields/'.$formtype.'/field_edit.inc.php';
				require_once MOD_ROOT.'formguide/createhtml.php';
				showmessage('操作成功！', $forward);
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		else 
		{
			if(!is_ie()) showmessage('本功能只支持IE浏览器，请用IE浏览器打开。');
			$infos = $formguide_fields->get($fieldid);
			@extract(new_htmlspecialchars($infos));
			$unsetgroups = form::checkbox($GROUP, 'unsetgroupids', 'unsetgroupids', $unsetgroupids, 5);
			include 'templates/fields_edit.tpl.php';
		}
		break;
	case 'fields_copy':
		if($dosubmit)
		{
			$info['formid'] = $formid;
			$info['formtype'] = $formtype;
			$info['unsetgroupids'] = isset($unsetgroupids) ? implodeids($unsetgroupids) : '';
			if(!$result = $formguide_fields->add($info, $setting))
			{
				showmessage($formguide_fields->msg(), 'goback');
			}
			if($result)
			{
				extract($setting);
				extract($info);
				require_once MOD_ROOT.'formguide/fields/'.$formtype.'/field_add.inc.php';
				showmessage('操作成功！', $forward);
			}
			else
			{
				showmessage('操作失败！');
			}
		}
		else
		{
			$info = $formguide_fields->get($fieldid);
			if(!$info) showmessage('指定的字段不存在！');
			@extract(new_htmlspecialchars($info));
			$unsetgroups = form::checkbox($GROUP, 'unsetgroupids', 'unsetgroupids', $unsetgroupids, 5);
			include 'templates/fields_copy.tpl.php';
		}		
	break;
	case 'fields_manage':
		$where = "formid='$formid'";
		$infos = $formguide_fields->listinfo($where);
		include 'templates/fields_manage.tpl.php';
		break;
	case 'fields_setting_add':
		require_once MOD_ROOT.'formguide/fields/'.$formtype.'/field_add_form.inc.php';
		break;
	case 'fields_setting_edit':
		$info = $formguide_fields->get($fieldid);
		if(!$info) showmessage('指定的字段不存在！');
		eval("\$setting = $info[setting];");
		@extract($setting);
		require_once MOD_ROOT.'formguide/fields/'.$formtype.'/field_edit_form.inc.php';
		break;
	case 'fields_delete':
		$info = $formguide_fields->get($fieldid);
		$formguide_fields->delete($fieldid);
		@extract($info);
		require_once MOD_ROOT.'formguide/fields/'.$formtype.'/field_delete.inc.php';
		showmessage('操作成功');
		break;
	case 'fields_listorder':
		$formguide_fields->listorder($info);
		showmessage('操作成功');
		break;
	case 'fields_preview':
		require CACHE_MODEL_PATH.'formguide_form.class.php';
		$formguide_form = new formguide_form($formid);
		$forminfos = $formguide_form->get();
		include 'templates/form_add.tpl.php';
		break;
	
	case 'fields_disabled':
		$formguide_fields->disabled($fieldid, $disabled);
		showmessage('操作成功', $forward);
		break;
	case 'fields_checkfield':
		if($formguide_fields->field_exsited($value, $formid, $fieldid))
		{
			exit('字段已存在');
		}
		elseif(!preg_match("/^[a-zA-Z_][a-zA-Z0-9_]+$/", $value))
		{
			exit('字符只能包含英文字母，数字和下划线');
		}
		else
		{
			exit('success');
		}
		break;
	case 'fields_checkname':
		if($formguide_fields->fieldname_exsited($value, $formid, $fieldid))
		{
			exit('字段名已存在');
		}
		else
		{
			exit('success');
		}
		break;
	
	case 'view_delete':
		if(empty($dataid)) showmessage('请选择需要删除的信息ID', $forward);
		if($formguide_fields->deleteinfo($dataid))
		{
			showmessage('操作成功', $forward);
		}
		else
		{
			showmessage('操作失败', $forward);
		}
	break;
	case 'view_manage':
		if(!class_exists('formguide_search_form'))
		{
			require CACHE_MODEL_PATH.'formguide_search_form.class.php';
		}
		$formname = $FORMGUIDE[$formid]['name'];
		$form = new formguide_search_form($formid);
		$where = $form->get_where();
		$page = max(intval($page), 1);
		$pagesize = $SETTING['pagesize'] ? $SETTING['pagesize'] : 30;	
		$forminfos = $formguide_form->get();
		$formname = $FORMGUIDE[$formid]['name'];
		$result = $formguide_fields->listview($page, $pagesize);
		include 'templates/view.tpl.php';
	break;
	
	case 'viewinfo':
		if(!$forward) $forward = HTTP_REFERER;
		if(!class_exists('formguide_admin'))
		{
			require MOD_ROOT.'admin/include/formguide_admin.class.php';
		}
		if(!class_exists('formguide_form'))
		{
			require CACHE_MODEL_PATH.'formguide_output.class.php';
		}
		$formguide_admin = new formguide_admin();
		$result = $formguide_admin->getbydataid($formid, $dataid);
		$formguide_output = new formguide_output($formid);
		$info = $formguide_output->get($result);
		include 'templates/viewinfo.tpl.php';
	break;
	
	case 'setting':
		if(!$forward) $forward = HTTP_REFERER;
		if($dosubmit)
		{
			module_setting($module, $setting);
			showmessage($LANG['save_setting_success'], $forward);
		}
		else
		{
			@extract(new_htmlspecialchars($M));
			include 'templates/setting.tpl.php';
		}
	break;
	
	case 'search':
		if(!$forward) $forward = '?module='.$module.'&file='.$file;
		require CACHE_MODEL_PATH.'formguide_search_form.class.php';
		$formname = $FORMGUIDE[$formid]['name'];
		$form = new formguide_search_form($formid);
		$where = $form->get_where();
		if($dosubmit)
		{
			if(!class_exists('member_search'))
			{
				require	CACHE_MODEL_PATH.'formguide_search.class.php';
			}
			$search = new formguide_search($formid);
			$data = $search->data($page, $SETTING['search_pagesize']);
			$pages = $search->pages;
			$total = $search->total;
			include 'templates/search_result.tpl.php';
		}
	break;
	
	case 'priv':
		if(!$forward) $forward = HTTP_REFERER;
		if($dosubmit)
		{
			$priv_role->update('module', $mod, $priv_roleid);
			showmessage($LANG['save_setting_success'], $forward);
		}
		else
		{
			//$privs = include MOD_ROOT.'include/priv.inc.php';
			$privs = array('all'=>array('name'=>'模块管理', 'file'=>'', 'action'=>''),
							 'add'=>array('name'=>'表单管理', 'file'=>'manage', 'action'=>'manage'),
							 'check'=>array('name'=>'表单添加', 'file'=>'manage', 'action'=>'add'),
							 'delete'=>array('name'=>'表单删除', 'file'=>'manage', 'action'=>'manage, delete'),
							 'view'=>array('name'=>'表单信息查看', 'file'=>'view, viewinfo', 'action'=>''),
							 'manage'=>array('name'=>'表单字段管理', 'file'=>'manage_fields', 'action'=>''),
							);
			include 'templates/priv.tpl.php';
		}
	break;
	
	case 'createhtml':
		require CACHE_MODEL_PATH.'formguide_form.class.php';
		$formguide = new formguide_form($formid);
		$forminfos = $formguide->get();
		$FORMGUIDE = cache_read('formguide.php');
		$f = $FORMGUIDE[$formid];
		$formname = $f['name'];
		$templateid = $f['template'];
		ob_start();
		include template('formguide_'.$templateid);
		$data = ob_get_contents();
		ob_clean();
		$file = WWW_ROOT.'sites/formguide/'.$formid.'.html';
		$strlen = file_put_contents($file, $data);
		@chmod($file,0777);
	break;
}
?>